ml-matrix
Matrix manipulation and computation library.
Maintained by Zakodium
Installation
$ npm install ml-matrix
Usage
As an ES module
import { Matrix } from 'ml-matrix';
const matrix = Matrix.ones(5, 5);
As a CommonJS module
const { Matrix } = require('ml-matrix');
const matrix = Matrix.ones(5, 5);
Examples
Standard operations
const { Matrix } = require('ml-matrix');
var A = new Matrix([
[1, 1],
[2, 2],
]);
var B = new Matrix([
[3, 3],
[1, 1],
]);
var C = new Matrix([
[3, 3],
[1, 1],
]);
Operations
const addition = Matrix.add(A, B);
const subtraction = Matrix.sub(A, B);
const multiplication = A.mmul(B);
const mulByNumber = Matrix.mul(A, 10);
const divByNumber = Matrix.div(A, 10);
const modulo = Matrix.mod(B, 2);
const maxMatrix = Matrix.max(A, B);
const minMatrix = Matrix.min(A, B);
Inplace Operations
C.add(A);
C.sub(A);
C.mul(10);
C.div(10);
C.mod(2);
Math Operations
var A = new Matrix([
[ 1, 1],
[-1, -1],
]);
var exponential = Matrix.exp(A);
var cosinus = Matrix.cos(A);
var absolute = Matrix.abs(A);
Available Methods:
abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, clz32, cos, cosh, exp, expm1, floor, fround, log, log1p, log10, log2, round, sign, sin, sinh, sqrt, tan, tanh, trunc
Manipulation of the matrix
var numberRows = A.rows;
var numberCols = A.columns;
var firstValue = A.get(0, 0);
var numberElements = A.size;
var isRow = A.isRowVector();
var isColumn = A.isColumnVector();
var isSquare = A.isSquare();
var isSym = A.isSymmetric();
A.set(1, 0, 10);
var diag = A.diag();
var m = A.mean();
var product = A.prod();
var norm = A.norm();
var transpose = A.transpose();
Instantiation of matrix
var z = Matrix.zeros(3, 2);
var z = Matrix.ones(2, 3);
var z = Matrix.eye(3, 4);
Maths
const {
Matrix,
inverse,
solve,
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition,
EigenvalueDecomposition,
} = require('ml-matrix');
Inverse and Pseudo-inverse
var A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
var inverseA = inverse(A);
var B = A.mmul(inverseA);
var A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
var inverseA = inverse(A, (useSVD = true));
var B = A.mmul(inverseA);
var A = new Matrix([
[1, 2],
[3, 4],
[5, 6],
]);
var pseudoInverseA = A.pseudoInverse();
var B = A.mmul(pseudoInverseA).mmul(A);
Least square
Least square is the following problem: We search for x
, such that A.x = B
(A
, x
and B
are matrix or vectors).
Below, how to solve least square with our function
var A = new Matrix([
[3, 1],
[4.25, 1],
[5.5, 1],
[8, 1],
]);
var B = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
var x = solve(A, B);
var error = Matrix.sub(B, A.mmul(x));
var A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
var B = Matrix.columnVector([8, 20, 32]);
var x = solve(A, B, (useSVD = true));
var error = Matrix.sub(B, A.mmul(x));
Decompositions
QR Decomposition
var A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
var QR = new QrDecomposition(A);
var Q = QR.orthogonalMatrix;
var R = QR.upperTriangularMatrix;
LU Decomposition
var A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
var LU = new LuDecomposition(A);
var L = LU.lowerTriangularMatrix;
var U = LU.upperTriangularMatrix;
var P = LU.pivotPermutationVector;
Cholesky Decomposition
var A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
var cholesky = new CholeskyDecomposition(A);
var L = cholesky.lowerTriangularMatrix;
Eigenvalues & eigenvectors
var A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
var e = new EigenvalueDecomposition(A);
var real = e.realEigenvalues;
var imaginary = e.imaginaryEigenvalues;
var vectors = e.eigenvectorMatrix;
Linear dependencies
var A = new Matrix([
[2, 0, 0, 1],
[0, 1, 6, 0],
[0, 3, 0, 1],
[0, 0, 1, 0],
[0, 1, 2, 0],
]);
var dependencies = linearDependencies(A);
License
MIT